package com.combetter.changxing.ui.guide;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.net.HttpRequest;
import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.ui.activity.LoginActivity;
import com.combetter.changxing.ui.activity.MainActivity;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.ToastUtils;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * 启动界面
 * <p>
 * Created by chenwenchao on 2018/2/24.
 */
public class LaunchActivity extends Activity {
    private FrameLayout frameLayout;
    public static final String IS_FIRST = "is_first";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launch);
        frameLayout = findViewById(R.id.flContent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        AlphaAnimation animation = new AlphaAnimation(0.5f, 1f);
        animation.setDuration(2000);
        frameLayout.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (true) {
                    if (MyApp.getInstance().getUserInfo() != null) {
                        MyApp.getInstance().resetUserInfo();
                        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                        finish();
                    } else {
                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                        finish();
                    }
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                } else {
                    // startActivity(new Intent(LaunchActivity.this, GuideActivity.class));
                }
            }
        });
    }

    /**
     * 自动登录
     */
    public void login() {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", MyApp.getInstance().getUserInfo().getToken());
        HttpRequest.getInstance().createService(APIService.class)
                .login(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .doOnError(throwable -> {
                    LogUtil.e(throwable.getMessage());
                    startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                    finish();
                })
                .subscribe(userHttpResult -> {
                    LogUtil.e("自动登录返回：", userHttpResult);
                    if (userHttpResult.getCode() == 200) {
                        User user = MyApp.getInstance().getUserInfo();
                        User newUser = userHttpResult.getData();
                        user.setManager_mail(newUser.getManager_mail());
                        user.setManager_phone(newUser.getManager_phone());
                        user.setToken(newUser.getToken());
                        MyApp.getInstance().getDatabase().userDao().insertUser(user);
                        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                        finish();
                    } else {
                        ToastUtils.error(userHttpResult.getMsg());
                        startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                        finish();
                    }
                });
    }
}
